package andy.ham;

/**
 * Created by admin on 2018/6/20.
 */
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import andy.ham.db.gen.LoginDao;
public class set extends Activity {
    private SQLiteDatabase db;
    private andy.ham.db.gen.DaoMaster daoMaster;
    private andy.ham.db.gen.DaoSession daoSession;
    private LoginDao ld;
    private Login login;
    private loginDaoHelper ldh;
    private Long num;

    private EditText usename;
    private EditText psw;
    private EditText pswc;
    private Button setconfirm;
    private String usename_text, psw_text, pswc_text;
    private List<Login> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set);
        initView();
        InitDAO_tag();
        loginListener();

    }

    private void InitDAO_tag() {
        andy.ham.db.gen.DaoMaster.DevOpenHelper helper1 = new andy.ham.db.gen.DaoMaster.DevOpenHelper(this, "login.db", null);
        db = helper1.getWritableDatabase();
        daoMaster = new andy.ham.db.gen.DaoMaster(db);//代表着一个数据库的连接
        daoSession = daoMaster.newSession();//可以让我们使用一些Entity的基本操作和获取Dao操作类
        ld = daoSession.getLoginDao();
        ldh = new loginDaoHelper(ld);
        list = new ArrayList<Login>();
        login = new Login();
    }

    private void initView() {
        usename = (EditText) findViewById(R.id.username);
        psw = (EditText) findViewById(R.id.psw);
        pswc = (EditText) findViewById(R.id.pswc);
        setconfirm = (Button) findViewById(R.id.loginc);
    }

    public void loginListener() {
        setconfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                usename_text = usename.getText().toString();
                psw_text = psw.getText().toString();
                pswc_text = pswc.getText().toString();
                if (usename_text.equals("")) {
                    Toast.makeText(set.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                } else {
                    if (psw_text.equals(pswc_text)) {
                        String usename = usename_text;
                        String password = psw_text;
                        login.setUsername(usename_text);
                        login.setPassword(psw_text);
//                        list.add(usename);
//                        list.add(password);
//                        ld.loadAll();
                        ld.insert(login);

                        Toast.makeText(set.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent0 = new Intent(set.this,LoginDiary.class);
                        startActivity(intent0);
                    } else {
                        Toast.makeText(set.this, "密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}

